/******************************************************************************
*	device/hid/hid.h
*	 by Alex Chadwick
*
*	A light weight implementation of the USB protocol stack fit for a simple
*	driver.
*
*	device/hid/hid.h contains definitions relating to generic human interface
*	devices. Information about the hid reports is in device/hid/report.h.
******************************************************************************/
#ifndef _DEVICE_HID_HID_H
#define _DEVICE_HID_HID_H

#include <types.h>
#include <usbd/device.h>

//------------------------------------------------------------------------------
// The possible types of hid reports.
// Defined in "HID Specification, Ver 1,11 27/Jun/2001" manual in 7.2.1
//
typedef
	enum HidReportType {
		Input   = 1,
		Output  = 2,
		Feature = 3,
	}
HidReportType;

#include <device/hid/report.h>  // report.h depends on HidReportType

//------------------------------------------------------------------------------
// The human interface device descriptor information.
// Defined in "HID Specification, Ver 1,11 27/Jun/2001" manual in 6.2.1
//
typedef
	enum HidCountry {
		CountryNotSupported =  0,
		Arabic              =  1,
		Belgian             =  2,
		CanadianBilingual   =  3,
		CanadianFrench      =  4,
		CzechRepublic       =  5,
		Danish              =  6,
		Finnish             =  7,
		French              =  8,
		German              =  9,
		Greek               = 10,
		Hebrew              = 11,
		Hungary             = 12,
		International       = 13,
		Italian             = 14,
		Japan               = 15,
		Korean              = 16,
		LatinAmerican       = 17,
		Dutch               = 18,
		Norwegian           = 19,
		Persian             = 20,
		Poland              = 21,
		Portuguese          = 22,
		Russian             = 23,
		Slovakian           = 24,
		Spanish             = 25,
		Swedish             = 26,
		SwissFrench         = 27,
		SwissGerman         = 28,
		Switzerland         = 29,
		Taiwan              = 30,
		TurkishQ            = 31,
		EnglishUk           = 32,
		EnglishUs           = 33,
		Yugoslavian         = 34,
		TurkishF            = 35,
	}
HidCountry;

typedef
	struct HdOptionalDescriptors {
		DescriptorTYPE Type : 8;  // +0x0
		u16 Length;               // +0x1
	}
HdOptionalDescriptors;

typedef
	struct HidDescriptor {
		u8                    DescriptorLength;      // +0x0
		DescriptorTYPE        DescriptorType : 8;    // +0x1
		u16                   HidVersion;            // +0x2 (BCD, Eg 0x210 => v2.10)
		HidCountry            Countrycode : 8;       // +0x4
		u8                    DescriptorCount;       // +0x5
		HdOptionalDescriptors OptionalDescriptors[]; // +0x6 (<= DescriptorCount optional descriptors)
	}
HidDescriptor;

//------------------------------------------------------------------------------
// The DeviceDriver field in UsbDriverDataHeader for hid devices.
//
#define DeviceDriverHid  (0x48494430)

//------------------------------------------------------------------------------
// Hid specific data.
//
// The contents of the driver data field for hid devices.
// Chains to allow a stacked driver.
//
typedef
	struct HidDevice {
		UsbDriverDataHeader Header;
		HidDescriptor       *Descriptor;
		HidParserResult     *ParserResult;
		UsbDriverDataHeader *DriverData;

		// HID event handlers
		void (*HidDetached  )(UsbDevice* device);
		void (*HidDeallocate)(UsbDevice* device);
	}
HidDevice;

#define HidUsageAttachCount  (10)

//------------------------------------------------------------------------------
// Methods to attach an interface of particular hid desktop usage.
//
// The application desktop usage of the interface is the index into this array
//   of methods.
// The array is populated by ConfigurationLoad().
//
extern Result (*HidUsageAttach[HidUsageAttachCount])(UsbDevice *device, u32 interfaceNumber);

//------------------------------------------------------------------------------
Result  HidGetReport (UsbDevice* device, HidReportType reportType,
                      u8 reportId, u8 interface, u32 bufferLength, void* buffer) ;

Result  HidSetReport (UsbDevice* device, HidReportType reportType,
                      u8 reportId, u8 interface, u32 bufferLength, void* buffer) ;

Result  HidWriteDevice (UsbDevice* device, u8 reportNumber) ;

Result  HidReadDevice (UsbDevice* device, u8 reportNumber) ;

Result  HidAttach (UsbDevice* device, u32 interfaceNumber) ;

#endif // _DEVICE_HID_HID_H
